﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace Hyunhdai_Saigon_Project
{
    public partial class Cap_Nhap_San_Pham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["TenDangNhap"] == null || Session["MatKhau"] == null)
            {
                Response.Redirect("login.aspx");
            }

            if (!IsPostBack)
                BindData();

        }

        protected void BindData()
        {
            string mx = Request.QueryString["p"];
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisg"].ConnectionString);
            connection.Open();
            string sql = "select * from xe where maxe = "+mx;
            SqlDataAdapter da = new SqlDataAdapter(sql, connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            connection.Close();
            
            switch(Convert.ToInt16(dt.Rows[0][0].ToString()))
            {
                case 1: nsx.SelectedIndex = 0;
                    break;
                case 2: nsx.SelectedIndex = 1;
                    break;
                case 3: nsx.SelectedIndex = 2;
                    break;
            }

            string tmp = dt.Rows[0][2].ToString();
            if(tmp.Equals("Xe Du Lịch"))
                dgx.SelectedIndex=0;
            else if (tmp.Equals("Xe Khách"))
                dgx.SelectedIndex=1;
            else dgx.SelectedIndex = 2;
            tx.Text = dt.Rows[0][3].ToString();
            dx.Text = dt.Rows[0][4].ToString();
            nt.Text = dt.Rows[0][5].ToString();
            sc.Text = dt.Rows[0][6].ToString();
            scn.Text = dt.Rows[0][7].ToString();
            hs.Text = dt.Rows[0][8].ToString();
            nl.Text = dt.Rows[0][9].ToString();
            htnl.Text = dt.Rows[0][10].ToString();
            ttnl.Text = dt.Rows[0][11].ToString();
            imgURL.Text = dt.Rows[0][12].ToString();
            Image1.ImageUrl = imgURL.Text;
            
            
        }
        SqlCommand cm;
        protected void XacNhan_Click(object sender, EventArgs e)
        {
             string mx = Request.QueryString["p"];
             try
             {
                 string[] sql = new string[12];
                 SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisg"].ConnectionString);
                 connection.Open();
                 sql[0] = "update xe set ";
                 sql[0] += "idnsx = " + (nsx.SelectedIndex + 1) + " where maxe =" + mx;
                 sql[2] = "update xe set ";
                 sql[2] += "dongxe = N'" + dgx.SelectedValue + "' where maxe =" + mx;
                 sql[3] = "update xe set ";
                 sql[3] += "doixe = " + dx.Text + " where maxe =" + mx;
                 sql[4] = "update xe set ";
                 sql[4] += "ngoaithat = N'" + nt.Text + "' where maxe =" + mx;
                 sql[5] = "update xe set ";
                 sql[5] += "socua = " + sc.Text + " where maxe =" + mx;
                 sql[6] = "update xe set ";
                 sql[6] += "sochongoi = " + scn.Text + " where maxe =" + mx;
                 sql[7] = "update xe set ";
                 sql[7] += "hopso = N'" + hs.Text + "' where maxe =" + mx;
                 sql[8] = "update xe set ";
                 sql[8] += "nhienlieu = N'" + nl.Text + "' where maxe =" + mx;
                 sql[9] = "update xe set ";
                 sql[9] += "htnhienlieu = N'" + htnl.Text + "' where maxe =" + mx;
                 sql[10] = "update xe set ";
                 sql[10] += "ttnhieulieu = N'" + ttnl.Text + "' where maxe =" + mx;
                 sql[11] = "update xe set ";
                 sql[11] += "imgURL = '" + imgURL.Text + "' where maxe =" + mx;
                 for (int i = 0; i < 12; i++)
                 {
                     if (i != 1)
                         cm = new SqlCommand(sql[i], connection);
                     cm.ExecuteNonQuery();
                 }
                 connection.Close();
                 xacnhan.Text = "Thông tin về xe được cập nhâp thành công";
             }
             catch (Exception)
             {
                 xacnhan.Text = "Thông tin về xe chưa được cập nhập";
             }
        }
        protected void DangXuat_Click(object sender, EventArgs e)
        {
            Session.RemoveAll();
            Response.Redirect("Default.aspx");
        }
        protected void ThemSp_Click(object sender, EventArgs e)
        {
            Response.Redirect("admin.aspx");
        }
    }
}